package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.OaHysglOaFlowConstants;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaXcxxspgl;
import com.yuncheng.oaApi.service.IOaXcxxspglService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 宣传信息审批管理
 * @Author: LHB
 * @Date: 2023-07-29
 * @Version: V1.0
 */
@Api(tags = "宣传信息审批管理接口")
@RestController
@RequestMapping("/api/spzx-oa/oaXcxxspgl")
public class OaXcxxspglController extends BaseController<OaXcxxspgl, IOaXcxxspglService> {

    private static final Logger log = LoggerFactory.getLogger(OaXcxxspglController.class);

    @Resource
    private IOaXcxxspglService oaXcxxspglService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    /**
     * 分页列表查询
     *
     * @param oaXcxxspgl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "宣传信息审批管理-分页列表查询")
    @ApiOperation(value = "宣传信息审批管理-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaXcxxspgl oaXcxxspgl, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaXcxxspgl> queryWrapper = QueryBuilder.initQueryWrapper(oaXcxxspgl, req.getParameterMap());
        Page<OaXcxxspgl> page = new Page<>(pageNo, pageSize);
        IPage<OaXcxxspgl> pageList = oaXcxxspglService.page(page, queryWrapper);
        for(OaXcxxspgl vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }



    /**
     * 添加
     *
     * @param oaXcxxspgl
     * @return
     */
    @AutoLog(value = "宣传信息审批管理-添加")
    @ApiOperation(value = "宣传信息审批管理-添加")
    //@RequiresPermissions("XCXXSP:OaXcxxspgl:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaXcxxspgl oaXcxxspgl) {
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (StringUtils.isBlank(oaXcxxspgl.getFngks())){
            oaXcxxspgl.setFngks(currentUser.getDeptName());
        }
        if (StringUtils.isBlank(oaXcxxspgl.getFngr())){
            oaXcxxspgl.setFngr(currentUser.getName());
        }
        if (StringUtils.isBlank(oaXcxxspgl.getFuserid())){
            oaXcxxspgl.setFuserid(currentUser.getId());
        }
        oaXcxxspglService.save(oaXcxxspgl);
        return HttpResult.ok(oaXcxxspgl);
    }
//

    /**
     * 编辑
     *
     * @param oaXcxxspgl
     * @return
     */
    @AutoLog(value = "宣传信息审批管理-编辑")
    @ApiOperation(value = "宣传信息审批管理-编辑")
    //@RequiresPermissions("XCXXSP:OaXcxxspgl:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaXcxxspgl oaXcxxspgl) {
        oaXcxxspglService.updateById(oaXcxxspgl);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }
//
//    /**
//     * 通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "宣传信息审批管理-通过id删除")
//    @ApiOperation(value = "宣传信息审批管理-通过id删除")
//    //@RequiresPermissions("XCXXSP:OaXcxxspgl:delete")
//    @DeleteMapping(value = "/delete")
//    public HttpResult<?> delete(@RequestParam("id") String id) {
//        oaXcxxspglService.removeById(id);
//        return HttpResult.ok(I18nUtil.message("delete.success"));
//    }
//
//    /**
//     * 通过id批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "宣传信息审批管理-批量删除")
//    @ApiOperation(value = "宣传信息审批管理-批量删除")
//    //@RequiresPermissions("XCXXSP:OaXcxxspgl:delete")
//    @DeleteMapping(value = "/deleteBatch")
//    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
//        oaXcxxspglService.removeByIds(Arrays.asList(ids.split(",")));
//        return HttpResult.ok(I18nUtil.message("delete.success"));
//    }
//
//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "宣传信息审批管理-通过id查询")
//    @ApiOperation(value = "宣传信息审批管理-通过id查询")
//    @GetMapping(value = "/queryById")
//    public HttpResult<?> queryById(@RequestParam("id") String id) {
//        OaXcxxspgl oaXcxxspgl = oaXcxxspglService.getById(id);
//        return HttpResult.ok(oaXcxxspgl);
//    }
//
//    /**
//     * 导出excel
//     */
//    @AutoLog(value = "宣传信息审批管理-导出excel")
//    @ApiOperation(value = "宣传信息审批管理-导出excel")
//    //@RequiresPermissions("XCXXSP:OaXcxxspgl:export")
//    @RequestMapping(value = "/exportExcel")
//    public ModelAndView exportExcel(HttpServletRequest request,  OaXcxxspgl oaXcxxspgl) {
//    	return super.exportExcel(request, oaXcxxspgl, OaXcxxspgl.class, "宣传信息审批管理");
//    }
//
//    /**
//     * excel导入数据
//     */
//    @AutoLog(value = "宣传信息审批管理-excel导入数据")
//    @ApiOperation(value = "宣传信息审批管理-excel导入数据")
//    //@RequiresPermissions("XCXXSP:OaXcxxspgl:import")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public HttpResult<?> importExcel(HttpServletRequest request) {
//    		return super.importExcel(request, OaXcxxspgl.class);
//    }

    /**
     * 数据转换
     * @param oaXcxxspgl
     */
    private void formatValue(OaXcxxspgl oaXcxxspgl){
    }


    @AutoLog(value = "导出宣传信息审批数据-表格excel")
    @ApiOperation(value = "导出宣传信息审批数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportxcxxspData")
    public void exportxcxxspData(@RequestParam(value = "ids",defaultValue = "")String ids,
                                OaXcxxspgl ndkpBasic,
                                HttpServletResponse response){
        try {
            String name = "exportxcxxspData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaXcxxspgl> ndkpBasicList = oaXcxxspglService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaXcxxspgl e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaXcxxspgl> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFuserid()),OaXcxxspgl::getFuserid,ndkpBasic.getFuserid());
                //拟稿人
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFngr()),OaXcxxspgl::getFngr, ndkpBasic.getFngr());
                //拟稿科室
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFngks()),OaXcxxspgl::getFngks,ndkpBasic.getFngks());
                //发布栏目
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFfblm()),OaXcxxspgl::getFfblm,ndkpBasic.getFfblm());
                //发布日期
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFfbrq()),OaXcxxspgl::getFfbrq,ndkpBasic.getFfbrq());



                if (StringUtils.isBlank(ndkpBasic.getFlczt())) {

                }else if (ndkpBasic.getFlczt().equals(OaHysglOaFlowConstants.HJMC_LCJS)){
                    queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFlczt()), OaXcxxspgl::getFlczt, OaHysglOaFlowConstants.HJMC_LCJS);
                }else {
                    queryWrapper.isNull(OaXcxxspgl::getFlczt);
                }
                if (ndkpBasic.getFfbrq() != null){
                    queryWrapper.le(OaXcxxspgl::getFfbrq,ndkpBasic.getFfbrq());
                }

                List<OaXcxxspgl> ndkpBasicList = oaXcxxspglService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaXcxxspgl e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_XCXXSP_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出宣传信息审批数据出错-{}",e.getMessage());
        }
    }


}
